import router from './router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

// NProgress Configuration
NProgress.configure({showSpinner: true})

// 路劲白名单
const whiteList = ['/form/token', '/register', '/first', '/test']

router.beforeEach(async (to, from, next) => {
    // start progress bar
    NProgress.start()
    // 获取token
    const hasToken = localStorage.getItem("token")
    if (hasToken) {
        if (to.path === '/first') {
            next({name: 'ShowLink'})
            NProgress.done()
        } else {
            next()
        }
    } else {
        /* has no token*/
        if (whiteList.indexOf(to.path) !== -1) {
            // in the free login whitelist, go directly
            next()
        } else {
            console.error(to.path)
            // other pages that do not have permission to access are redirected to the login page.
            next({name: "FirstLink"})
            NProgress.done()
        }
    }
})

router.afterEach(() => {
    // finish progress bar
    NProgress.done()
})
